<?php

declare(strict_types=1);
/**
 * This file is part of bearing-web.
 *
 * @link     https://www.bearing.cn/
 * @contact  tech@bearing.cn
 * @license  https://gitee.com/cppo/bearing_web/-/blob/master/README.md
 */

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;

/**
 * This is the model class for table "t_news".
 *
 * @property int $id
 * @property string $name
 * @property null|string $thumb
 * @property null|string $brief
 * @property string $content
 * @property null|string $seo_title
 * @property null|string $seo_keyword
 * @property null|string $seo_desc
 * @property int $status 状态
 * @property int $cate_id 分类
 * @property null|string $source 来源
 * @property int $lan 语言
 * @property int $create_time
 * @property int $update_time
 */
class NewsModel extends ActiveRecord
{
    public static function tableName(): string
    {
        return '{{%news}}';
    }

    public function rules(): array
    {
        return [
            [['name', 'content', 'create_time', 'update_time'], 'required'],
            [['content'], 'string'],
            [['status', 'cate_id', 'lan', 'create_time', 'update_time'], 'integer'],
            [['name', 'thumb', 'brief', 'seo_title', 'seo_keyword', 'seo_desc', 'source'], 'string', 'max' => 255],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'thumb' => 'Thumb',
            'brief' => 'Brief',
            'content' => 'Content',
            'seo_title' => 'Seo Title',
            'seo_keyword' => 'Seo Keyword',
            'seo_desc' => 'Seo Desc',
            'status' => '状态',
            'cate_id' => '分类',
            'source' => '来源',
            'lan' => '语言',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    /**
     * 时间更新策略.
     *
     * @return array[]
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],   // 创建
                    BaseActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],       // 修改
                ],
                'value' => function () {
                    return time();
                },
            ],
        ];
    }
}
